package com.squid.base.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author jh
 * date 2023/6/12 15:21
 */
public class RandomUtils {
    /**
     * 得到uuid
     *
     * @return {@link String }
     * @author jh
     * @date 2023/06/06
     */
    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 获取指定文字的随机中文
     *
     * @param hanZi 汉滋
     * @return {@link String }
     * @author jh
     * date 2023/06/12
     */
    public static String getRandomHan(String hanZi) {
        return String.valueOf(hanZi.charAt(new Random().nextInt(hanZi.length())));
    }

    /**
     * 得到随机整数
     *
     * @param bound 绑定
     * @return int
     * @author jh
     * date 2023/06/12
     */
    public static int getRandomInt(int bound) {
        return ThreadLocalRandom.current().nextInt(bound);
    }

    /**
     * 获取随机中文
     *
     * @return {@link String }
     * @author jh
     * date 2023/06/12
     */
    public static String getRandomHan() {
        String str = "";
        int highCode;
        int lowCode;

        Random random = new Random();
        //B0 + 0~39(16~55) 一级汉字所占区
        highCode = (176 + Math.abs(random.nextInt(39)));
        //A1 + 0~93 每区有94个汉字
        lowCode = (161 + Math.abs(random.nextInt(93)));
        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 随机范围内数字
     *
     * @param startNum 开始num
     * @param endNum   结束num
     * @return {@link Integer }
     * @author jh
     * date 2023/06/12
     */
    public static Integer getRandomInt(int startNum, int endNum) {
        return ThreadLocalRandom.current().nextInt(endNum - startNum) + startNum;
    }

    /**
     * 获取随机字符串
     *
     * @param length 长度
     * @return {@link String }
     * @author jh
     * date 2023/06/12
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 获得雪花id，string
     *
     * @return 雪花id，string
     */
    public static String getSnowFlakeString() {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        return snowflake.nextIdStr();
    }

    /**
     * 雪花id，long
     *
     * @return 雪花id，long
     */
    public static Long getSnowFlakeLong() {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        return snowflake.nextId();
    }
}